<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Category extends ShopBackEndController {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('repo/shop_category_model', 'shop_category_model');
        $this->load->model('repo/category_model', 'category_model');
    }

    public function index()
    {
        $aParentCate = $this->category_model->getCategories();
        $aCategory = $this->shop_category_model->getAll(array(), array(), $this->shop->getId());
        $aReturn = array(
            'pageTitle'   => 'Danh mục',
            'aParentCate' => $aParentCate,
            'aCategory'   => $aCategory);
        $this->renderView('backend/shop/category/index', $aReturn);
    }

    public function get($slug)
    {
        $this->load->model('repo/shop_model', 'shop_model');
        $aParentCate = $this->category_model->getCategories();
        $category    = $this->shop_category_model->get($slug, $this->shop->getId());
        if ($category == null || count($category) == 0) {
            show_404();
        }
        $aReturn = array(
            'pageTitle'   => $category->getTitle(),
            'category'    => $category,
            'aParentCate' => $aParentCate,
            'action_link' => 'cap-nhat');
        $this->renderView('backend/shop/category/single', $aReturn);
    }

    private function setValidate()
    {
        $this->form_validation->set_rules('category[title]', 'lang:category_title', 'trim|required|max_length[100]|xss_clean');
        $this->form_validation->set_rules('category[slug]',  'lang:category_slug',  'trim|required|callback_slug_check|xss_clean');
        $this->form_validation->set_rules('category[order]', 'lang:category_order', 'trim|required|numeric|xss_clean');
        $this->form_validation->set_error_delimiters('<li>', '</li>');
    }

    public function doUpdate()
    {
        $aResult = array();
        $date = new DateTime();
        $category = $this->input->post('category', true);

        //validate form
        $this->setValidate();
        if ($this->form_validation->run() == false) {
            $aResult = array(
                'code'  => 0,
                'title' => $this->lang->line('update_fail'),
                'text'  => '<ul>' . validation_errors() . '</ul>',
                );
            $this->renderJSON($aResult);
            return;
        }

        //set id by decode id
        $category_id = false;
        if (isset($category['cate_flh'])) {
            $categoryTempFlh = $this->input->post('category');
            $category_id = $this->encrypt->decode($categoryTempFlh['id']);
            $category['cate_id'] = $categoryTempFlh['cate_flh'];
            unset($category['cate_flh']);
        } elseif (isset($category['cate'])) {
            $categoryTemp = $this->input->post('category');
            $category_id = $this->encrypt->decode($categoryTemp['id']);
            $category['cate_id'] = $this->encrypt->decode($categoryTemp['cate']);
            unset($category['cate']);
        } else {

        }
        if ($category_id == false) {
            $aResult = array(
                'code'  => 0,
                'title' => $this->lang->line('update_fail'),
                'text'  => '<ul><li>' . $this->lang->line('shop_category_id_invalid') . '</li></ul>'
                );
            $this->renderJSON($aResult);
            return;
        }

        //check info exist
        $category['shop_id'] = $this->shop->getId();
        $iExist = $this->shop_category_model->checkDuplicateCategory($this->shop->getId(), $category, $category_id);
        if ($iExist > 0) {
            $aResult = array(
                'code'  => 0,
                'title' => $this->lang->line('update_fail'),
                'text'  => '<ul><li>' . $this->lang->line('shop_category_exist') . '</li></ul>',
                );
            $this->renderJSON($aResult);
            return;
        }

        unset($category['id']);
        
        //set status
        if (isset($category['delete_flg']) && $category['delete_flg'] == 1) {
            $category['deleted_at'] = $date->format(DATE_TIME_FORMAT);
            unset($category['delete_flg']);
        } else {
            $category['deleted_at'] = null;
            $category['updated_at'] = $date->format(DATE_TIME_FORMAT);
            unset($category['delete_flg']);
        }

        //update
        $result = $this->shop_category_model->update($category_id, $category);
        if ($result) {
            $aResult = array(
                'code'       => 1, 
                'text'       => $this->lang->line('update_success'),
                'back_href'  => mns_category_single_url($category['slug']),
                'front_href' => shop_cate_url($this->shop->getSlug(), $category['slug'])
                );
        } else {
            $aResult = array(
                'code'  => 0,
                'title' => $this->lang->line('system_error'));
        }
        $this->renderJSON($aResult);
    }

    public function insert()
    {
        $this->load->model('repo/category_model', 'category_model');
        $aParentCate = $this->category_model->getCategories();
        $aReturn = array(
            'pageTitle'   => 'Thêm',
            'aParentCate' => $aParentCate,
            'action_link' => 'them-moi');
        $this->renderView('backend/shop/category/single', $aReturn);
    }

    public function doInsert()
    {
        $aResult = array();
        $date = new DateTime();
        $category = $this->input->post('category', true);

        $this->setValidate();
        if ($this->form_validation->run() == false) {
            $aResult = array(
                'code'  => 0,
                'title' => $this->lang->line('insert_fail'),
                'text'  => '<ul>' . validation_errors() . '</ul>',
                );
            $this->renderJSON($aResult);
            return;
        }
        
        //set shop_id
        $shop_id = $this->shop->getId();
        $category['shop_id'] = $shop_id;
        $iExist = $this->shop_category_model->checkDuplicateCategory($shop_id, $category);
        if ($iExist > 0) {
            $aResult = array(
                'code'  => 0,
                'title' => $this->lang->line('insert_fail'),
                'text'  => '<ul><li>' . $this->lang->line('shop_category_exist') . '</li></ul>',
                );
            $this->renderJSON($aResult);
            return;
        }
        if (isset($category['delete_flg']) && $category['delete_flg'] == 1) {
            $category['deleted_at'] = $date->format(DATE_TIME_FORMAT);
            unset($category['delete_flg']);
        } else {
            $category['created_at'] = $date->format(DATE_TIME_FORMAT);
            unset($category['delete_flg']);
        }

        //set parent_category
        if (isset($category['cate'])) {
            $categoryTemp = $this->input->post('category');
            $category['cate_id'] = $this->encrypt->decode($categoryTemp['cate']);
            unset($category['cate']);
        }
        
        //insert
        $result = $this->shop_category_model->insert($category);
        if ($result) {
            $aResult = array('code' => 1, 'text' => $this->lang->line('insert_success'), 'update_url'=>mns_category_single_url($category['slug']));
        } else {
            $aResult = array(
                'code'  => 0,
                'title' => $this->lang->line('system_error'),
                'text'  => $this->lang->line('insert_fail'));
        }
        $this->renderJSON($aResult);
    }

    public function flashUpdate()
    {
        $aData = array();
        $aResult = array();
        $date = new DateTime();
        $category = $this->input->post('category');
        $category_id = $this->encrypt->decode($category['id']);
        if ($category_id != false) {
            //set status
            if (isset($category['publish_flg']) && $category['publish_flg'] == 1) {
                $aData['deleted_at'] = null;
                $aData['updated_at'] = $date->format(DATE_TIME_FORMAT);
            } else {
                $aData['deleted_at'] = $date->format(DATE_TIME_FORMAT);
            }

            //update
            $result = $this->shop_category_model->update($category_id, $aData);
            if ($result) {
                $aResult = array('code' => 1, 'text' => $this->lang->line('update_success'));
            } else {
                $aResult = array(
                    'code' => 0,
                    'title' => $this->lang->line('system_error')
                    );
            }
        } else {
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('system_error')
                );
        }
        $this->renderJSON($aResult);
    }
}
?>